package com.fastCms.business.bim.manage.impl;


import com.fastCms.business.frame.constant.iotApi.IotApiConst;
import com.fastCms.business.bim.daos.DataProdMapper;
import com.fastCms.business.bim.daos.PollutionMapper;
import com.fastCms.business.bim.manage.PollutionManageService;
import com.fastCms.business.bim.pojo.entity.*;
import com.fastCms.business.bim.pojo.po.PollutionAddPO;
import com.fastCms.business.bim.pojo.po.PollutionPO;
import com.fastCms.business.bim.pojo.po.ProdChartPO;
import com.fastCms.business.bim.pojo.vo.*;
import com.fastCms.business.bim.service.AreaService;
import com.fastCms.business.bim.service.DeviceService;
import com.fastCms.business.bim.service.PollutionService;
import com.fastCms.business.bim.service.WorkduceService;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.fastCms.business.frame.utils.CommonsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;

import static com.fastCms.business.frame.constant.iotApi.IotApiConst.Cems_DEVICE;

@Transactional(rollbackFor = Exception.class)
@Component
@Slf4j
public class PollutionManageServiceImpl implements PollutionManageService {

    @Autowired
    private PollutionService pollutionServiceImpl;

    @Autowired
    private AreaService areaServiceImpl;

    @Autowired
    private DeviceService deviceServiceImpl;

    @Autowired
    private PollutionMapper pollutionMapper;

    @Autowired
    private WorkduceService workduceServiceImpl;
    @Autowired
    private DataProdMapper dataProdMapper;

    @Override
    public ApiResponse<PageInfo<PollutionVO>> showList(PageParam<PollutionPO> pageParam) {
        PollutionPO queryFields = pageParam.getQueryFields();
        PageInfo<DataProdVO> result = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
            .doSelectPageInfo(() ->
                pollutionMapper.showList(queryFields));
        return ResponseUtil.success(result);
    }

    @Override
    public ApiResponse<Boolean> saveOrUpdate(PollutionAddPO pollutionAddPO) {
        if (null == pollutionAddPO.getId()) {
            Pollution pollution = new Pollution();
            CommonsUtil.initEntity(pollution);
            BeanUtils.copyProperties(pollutionAddPO, pollution);
            boolean a = pollutionServiceImpl.save(pollution);
            return ResponseUtil.success(a);
        }
        Pollution pollution = pollutionServiceImpl.findById(pollutionAddPO.getId());
        BeanUtils.copyProperties(pollutionAddPO, pollution);
        pollutionServiceImpl.updateById(pollution);
        return ResponseUtil.success(true);
    }

    @Override
    public ApiResponse<PollutionVO> getDetail(Integer id) {
        PollutionVO pollutionVO = new PollutionVO();
        Pollution pollution = pollutionServiceImpl.getById(id);
        BeanUtils.copyProperties(pollution, pollutionVO);
        if (!Objects.isNull(pollution.getAreaId())) {
            Area area = areaServiceImpl.findById(pollution.getAreaId());
            pollutionVO.setAreaName(area.getAreaName());
        }
        ArrayList<Integer> deviceIds = Lists.newArrayList();
        deviceIds.add(pollution.getListenDeviceId());
        deviceIds.add(pollution.getProdDeviceId());
        deviceIds.add(pollution.getRepairDeviceId());
        List<Device> deviceList = deviceServiceImpl.findByIds(deviceIds);

        Map<Integer, Device> deviceMap = BeanConvertUtil.convertList2Map(deviceList, Device::getId, Function.identity());
        if (!Objects.isNull(pollution.getListenDeviceId())) {
            Device device = deviceMap.get(pollution.getListenDeviceId());
            if (null != device) {
                pollutionVO.setListenDeviceName(device.getDeviceName());
                pollutionVO.setListenDeviceCode(device.getDeviceCode());
                pollutionVO.setListenDeviceUid(device.getDeviceUid());
            }
        }
        if (!Objects.isNull(pollution.getProdDeviceId())) {
            Device device = deviceMap.get(pollution.getProdDeviceId());
            if (null != device) {
                pollutionVO.setProdDeviceName(device.getDeviceName());
                pollutionVO.setProdDeviceCode(device.getDeviceCode());
                pollutionVO.setProdDeviceUid(device.getDeviceUid());

                if (!Objects.isNull(device.getWorkduceId())) {
                    Workduce workduce = workduceServiceImpl.findById(device.getWorkduceId());
                    if (null != workduce) {
                        pollutionVO.setMaters(workduce.getMaters());
                        pollutionVO.setProduce(workduce.getProduce());
                    }

                }
            }

        }
        if (!Objects.isNull(pollution.getRepairDeviceId())) {
            Device device = deviceMap.get(pollution.getRepairDeviceId());
            if (null != device) {
                pollutionVO.setRepairDeviceName(device.getDeviceName());
                pollutionVO.setRepairDeviceCode(device.getDeviceCode());
                pollutionVO.setRepairDeviceUid(device.getDeviceUid());
            }
        }

        return ResponseUtil.success(pollutionVO);
    }

    @Override
    public ApiResponse<Boolean> delete(Integer id) {
        boolean b = pollutionServiceImpl.removeById(id);
        return ResponseUtil.success(b);
    }

    @Override
    public ApiResponse<List<NameAndNumVO>> numChart() {
//        @ApiModelProperty(value = "排放源类型:WLYS-物料封闭运输;WLCC-物料封闭存储；SCGY-生产工艺过程")

        int wlysNum = pollutionServiceImpl.countByType("WLYS");
        int wlccNum = pollutionServiceImpl.countByType("WLCC");
        int scgyNum = pollutionServiceImpl.countByType("SCGY");
        List<NameAndNumVO> objects = Lists.newArrayList();
        NameAndNumVO nameAndNumVO1 = new NameAndNumVO();
        nameAndNumVO1.setCode("WLYS");
        nameAndNumVO1.setName("物料封闭运输");
        nameAndNumVO1.setNum(wlysNum);
        NameAndNumVO nameAndNumVO2 = new NameAndNumVO();
        nameAndNumVO2.setCode("wlccNum");
        nameAndNumVO2.setName("物料封闭存储");
        nameAndNumVO2.setNum(wlccNum);
        NameAndNumVO nameAndNumVO3 = new NameAndNumVO();
        nameAndNumVO3.setCode("SCGY");
        nameAndNumVO3.setName("生产工艺过程");
        nameAndNumVO3.setNum(scgyNum);
        objects.add(nameAndNumVO1);
        objects.add(nameAndNumVO2);
        objects.add(nameAndNumVO3);
        return ResponseUtil.success(objects);
    }

    @Override
    public ApiResponse<List<PollutionChartVO>> getChartDetail(Integer id) {

        Pollution pollution = pollutionServiceImpl.findById(String.valueOf(id));
        if (pollution == null) {
            return null;
        }
        ApiResponse<PollutionVO> response = getDetail(id);
        PollutionVO pollutionVO = response.getData();

        List<PollutionChartVO> pollutionChartVOS = new ArrayList<>();
        PollutionChartVO pollutionChartVO2 = new PollutionChartVO();
        PollutionChartVO pollutionChartVO = new PollutionChartVO();

        List<ChartList> chart = new ArrayList<>();
        pollutionChartVO.setField("CEMS");

        List<ChartList> chart2 = new ArrayList<>();
        pollutionChartVO2.setField("生产设备");

        PollutionChartVO pollutionChartVO3 = new PollutionChartVO();
        List<ChartList> chart3 = new ArrayList<>();
        pollutionChartVO3.setField("治理设备");

        switch (pollution.getPollutionName()) {

            case "石灰窑烟囱":
                chart.add(new ChartList("烟气流速", "Device1:400003"));
                chart.add(new ChartList("烟气温度", "Device1:400005"));
                chart.add(new ChartList("烟气湿度", "Device1:400011"));

                chart2.add(new ChartList("煤气流量", "通道 1.石灰窑.BHYSHSBHYMQLL"));
                chart3.add(new ChartList("石灰窑窑顶除尘器电流", "通道 1.石灰窑2.SHYBHYCPCCQDL"));
                chart3.add(new ChartList("石灰窑窑顶除尘器频率", "通道 1.石灰窑2.SHYBHYCPCCQPL"));
                chart3.add(new ChartList("石灰窑窑顶除尘器压差", "通道 1.石灰窑2.SHYBHYYDCCQDL"));
//                done
                break;

            case "发电厂烟囱":
                chart.add(new ChartList("电厂烟气流速", "Device2:400003"));
                chart.add(new ChartList("电厂烟气温度", "Device2:400005"));
                chart.add(new ChartList("电厂烟气压力", "Device2:400009"));
                chart2.add(new ChartList("发电机", "Device2:400009"));

                chart3.add(new ChartList("GGH原烟气进口压力", "通道 1.脱硝.AIZ.PT101"));
                chart3.add(new ChartList("GGH净出口压力", "通道 1.脱硝.AIZ.PT102"));
                chart3.add(new ChartList("脱硝塔3层压力", "通道 1.脱硝.AIZ.PT103"));
                break;
//                done
            case "烧结机头烟囱":
                chart.add(new ChartList("脱硫塔二氧化硫折算", "Device0:400021"));
                chart.add(new ChartList("脱硫塔氮氧化物折算", "Device0:400025"));
                chart.add(new ChartList("脱硫塔烟气温度", "Device0:400005"));
                chart2.add(new ChartList("台机车速", "通道 1.烧结2.SJJTCJS"));
                chart2.add(new ChartList("主抽风机入口温度", "通道 1.烧结除尘.SJJZCFJRKWD"));
                chart2.add(new ChartList("抽风机风门", "通道 1.烧结除尘.SJJZCFJFM"));
                chart3.add(new ChartList("1#电场二次电压", "通道 1.烧结2.SJJTJDCC1#DCECDY"));
                chart3.add(new ChartList("2#电场二次电压", "通道 1.烧结2.SJJTJDCC2#DCECDY"));
                chart3.add(new ChartList("1#电场二次电流", "通道 1.烧结2.SJJTJDCC1#DCRCDL"));
                chart3.add(new ChartList("GGH原烟气进口压力", "通道 1.脱硝.AIZ.PT101"));
                chart3.add(new ChartList("GGH净出口压力", "通道 1.脱硝.AIZ.PT102"));
                chart3.add(new ChartList("脱硝塔3层压力", "通道 1.脱硝.AIZ.PT103"));
                break;
            case "烧结机机尾烟囱":
                chart.add(new ChartList("烟气温度", "Device3:400003"));
                chart.add(new ChartList("烟气湿度", "Device3:400007"));
                chart.add(new ChartList("烟气压力", "Device3:400009"));
                chart2.add(new ChartList("台机车速", "通道 1.烧结2.SJJTCJS"));
                chart2.add(new ChartList("主抽风机入口温度", "通道 1.烧结除尘.SJJZCFJRKWD"));
                chart2.add(new ChartList("主抽风机风门", "通道 1.烧结除尘.SJJZCFJFM"));
                chart3.add(new ChartList("机尾除尘电流", "通道 1.烧结除尘.SJCSJJWCCDL"));
                chart3.add(new ChartList("机尾除尘压差", "通道 1.烧结2.SJJCCYC"));
                break;
//                done
            case "高炉矿槽除尘烟囱":
                chart.add(new ChartList("烟气温度", "Device4:400003"));
                chart.add(new ChartList("烟气湿度", "Device4:400007"));
                chart.add(new ChartList("烟气压力", "Device4:400009"));
                chart2.add(new ChartList("高炉热风炉冷风流量", "通道 1.高炉3.GLRFLLFLL"));
                chart2.add(new ChartList("高炉出铁测温变化1#", "通道 1.高炉3.GLCTCWBH1#"));
                chart3.add(new ChartList("矿槽除尘器电流", "通道 1.烧结除尘.LTCGLKCCCQPL"));
                chart3.add(new ChartList("矿槽除尘器频率", "通道 1.烧结除尘.LTCGLKCCCQYC"));
                chart3.add(new ChartList("矿槽除尘器压差", "通道 1.烧结除尘.LTCGLKCCCQYC"));
                break;
//                done
            case "高炉出铁厂烟囱":
                chart.add(new ChartList("烟气温度", "Device5:400003"));
                chart.add(new ChartList("烟气湿度", "Device5:400007"));
                chart.add(new ChartList("烟气压力", "Device5:400009"));
                chart2.add(new ChartList("发电机", "Device2:400009"));
                chart3.add(new ChartList("出铁厂除尘电压", "通道 1.烧结2.SJJTJDCC1#DCECDY"));
                chart3.add(new ChartList("出铁厂除尘电压", "通道 1.烧结2.SJJTJDCC2#DCECDY"));
                chart3.add(new ChartList("出铁厂除尘电流", "通道 1.烧结2.SJJTJDCC1#DCRCDL"));
                break;

            default:
                break;

        }
        pollutionChartVO.setChartList(chart);
        pollutionChartVOS.add(pollutionChartVO);

        pollutionChartVO2.setChartList(chart2);
        pollutionChartVOS.add(pollutionChartVO2);


        pollutionChartVO3.setChartList(chart3);
        pollutionChartVOS.add(pollutionChartVO3);


        return ResponseUtil.success(pollutionChartVOS);
    }

    @Override
    public ApiResponse<List<CemsVO>> getCemsList() {
        List<CemsVO> list = new ArrayList<>();

        for (String key : Cems_DEVICE.keySet()) {
            CemsVO cemsVO = new CemsVO();
            cemsVO.setName(key);
            List<IotApiConst.CemsVO> cemsVOS = Cems_DEVICE.get(key);
            cemsVOS.stream().forEach(cemsVO1 -> {
                ProdChartPO prodChartPO = new ProdChartPO();
                DataProd dataProd;
                switch (cemsVO1.getType()) {
                    case "SO":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setSO("-" + "/35");
                        } else {
                            cemsVO.setSO(dataProd.getRemark() + "/35");
                        }
                        break;
                    case "NO":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setNO("-" + "/50");
                        } else {
                            cemsVO.setNO(dataProd.getRemark() + "/50");
                        }
                        break;
                    case "颗粒物":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setKl("-" + "/10");
                        } else {
                            cemsVO.setKl(dataProd.getRemark() + "/10");
                        }
                        break;
                    case "烟气流量":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setYq("-");
                        } else {
                            cemsVO.setYq(dataProd.getRemark());
                        }
                        break;
                    case "氧气":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setO2("-");
                        } else {
                            cemsVO.setO2(dataProd.getRemark());
                        }
                        break;
                    case "温度":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setWd("-");
                        } else {
                            cemsVO.setWd(dataProd.getRemark());
                        }
                        break;
                    case "湿度":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setSd("-");
                        } else {
                            cemsVO.setSd(dataProd.getRemark());
                        }
                        break;
                    case "压力":
                        prodChartPO.setDeviceId(cemsVO1.getProdId());
                        dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                        if (dataProd == null || StringUtils.isEmpty(dataProd.getRemark())) {
                            cemsVO.setYl("-");
                        } else {
                            cemsVO.setYl(dataProd.getRemark());
                        }
                        break;
                }
            });
            list.add(cemsVO);
        }
        return ResponseUtil.success(list);
    }


}
